/*
ID: icerupt1
PROG: comehome
LANG: C++11
*/

/* solution
 *
 * easy shortest path.
 *
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <deque>
#include <cctype>

std::ifstream fin {"comehome.in" };
std::ofstream fout{"comehome.out"};

int const no_path = 1<<25;
int const n = 52;
int map[n][n];
int dis[n];
bool cow[n];
bool vis[n];
int m;

int index(char ch)
{
	if (std::isupper(ch)) return ch - 'A' + 26;
	return ch - 'a';
}

void spfa(int source)
{
	for (int i = 0; i < n; i++) { dis[i] = no_path; map[i][i] = 0; }
	dis[source] = 0;
	std::deque<int> relaxed;
	relaxed.push_back(source);
	vis[source] = true;
	while (!relaxed.empty()) {
		int x = relaxed.front();
		relaxed.pop_front();

		for (int i = 0; i < n; i++)
			if (dis[i] > dis[x] + map[x][i]) {
				dis[i] = dis[x] + map[x][i];
				if (!vis[i]) {
					//slf
					if (!relaxed.empty() && dis[i] < dis[relaxed.front()])
						relaxed.push_front(i);
					else
						relaxed.push_back(i);
					vis[i] = true;
				}
			}

		vis[x] = false;
	}
}

int main()
{
	fin >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) map[i][j] = no_path;
	for (int i = 0; i < m; i++) {
		char x, y;  int z;
		fin >> x >> y >> z;
		if (std::isupper(x)) cow[x - 'A'] = true;
		if (std::isupper(y)) cow[y - 'A'] = true;
		int tx = index(x), ty = index(y);
		if (tx != ty) map[tx][ty] = map[ty][tx] = std::min(map[tx][ty], z);
	}

	spfa(n-1);
	int fastest_cow = no_path;
	char pos = '+';
	for (int i = 26; i < n-1; i++)
		if (cow[i - 26] && dis[i] < fastest_cow) {
			pos = i - 26 + 'A';
			fastest_cow = dis[i];
		}
	std::cout << pos << ' ' << fastest_cow << '\n';
	fout << pos << ' ' << fastest_cow << '\n';
}
